<style lang="less">
    @import "./login.less";
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long :loading="isLoading">登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip">大爱妙音-后台管理</p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                isLoading: false,
                form: {
                    userName: "iview_admin",
                    password: ""
                },
                rules: {
                    userName: [{
                        required: true,
                        message: "账号不能为空",
                        trigger: "blur"
                    }],
                    password: [{
                        required: true,
                        message: "密码不能为空",
                        trigger: "blur"
                    }]
                }
            };
        },
        created() {
            this.storage.get("login_user").then(login_user => {
                console.log(login_user);
                this.form.userName = login_user;
            });
        },
        methods: {
            handleSubmit() {
                this.$refs.loginForm.validate(valid => {
                    if (valid) {
                        this.$nextTick(() => {
                            this.isLoading = true;
                            this.storage.set("login_user", this.form.userName);
                            this.http
                                .post(this.postUrl.login, {
                                    username: this.form.userName,
                                    password: this.form.password
                                })
                                .then(
                                    token => {
                                        console.log(token);
                                        if (token.code >= 0) {
                                            this.storage.set("login_user", this.form.userName);
                                            this.http
                                                .post(this.postUrl.getAdminInfo, {
                                                    token: token.data
                                                })
                                                .then(data => {
                                                    if (data.code >= 0) {
                                                        if (data.data.is_supper_admin == 1) {
                                                            localStorage.setItem("access", 0);
                                                        } else {
                                                            this.$store.commit("updateMenulist", JSON.parse(data.data.role_info));
                                                        }
                                                        this.$store.commit("updateUser", data.data);
                                                        localStorage.setItem('permissions', data.data.role_info)
                                                        this.$router.push({
                                                            name: "home_index"
                                                        });
                                                    } else {
                                                        this.errorHandle(data.msg);
                                                        this.isLoading = false;
                                                    }
                                                });
                                        } else {
                                            this.errorHandle(token.msg);
                                            this.isLoading = false;
                                        }
                                    },
                                    () => {
                                        this.errorHandle();
                                        this.isLoading = false;
                                    }
                                );
                        });
                    }
                });
            }
        }
    };
</script>

<style>

</style>